package cn.servlet;

import cn.dao.StaffDao;
import cn.dao.StaffDaoImpl;
import cn.entity.Staff;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/add")
public class AddServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req,
                           HttpServletResponse resp)
            throws IOException {
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        String job = req.getParameter("job");
        int salary = Integer.parseInt(req.getParameter("salary"));
        //封装成staff对象
        Staff staff = new Staff(name, job, salary);
        StaffDao dao = new StaffDaoImpl();
        boolean flag = dao.addData(staff);
        if (!flag) {
            resp.sendRedirect("fail.jsp");
        } else {
            resp.sendRedirect("staff_list");
        }
    }
}
